{
 "metadata": {
  "language": "Julia",
  "name": "",
  "signature": "sha256:3c54da1dd4792d77eb3618f9b060d869a6f1f090cdf72dd8c06a803f1167dfae"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Cav Examples"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "A notebook full of examples of **Cav**. These examples will get you started in no time, and it also serves as an API reference.\n",
      "\n",
      "Start by placing the following line in your program:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "using Cav"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "is_pos, is_neg, is_zero, is_one"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [
      "{is_pos(1), is_pos(0), is_pos(-1), is_pos([-1,0,1])}"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 2,
       "text": [
        "4-element Array{Any,1}:\n",
        "  true                      \n",
        " false                      \n",
        " false                      \n",
        "      Bool[false,false,true]"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [
      "{is_neg(1), is_neg(0), is_neg(-1), is_neg([-1,0,1])}"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 3,
       "text": [
        "4-element Array{Any,1}:\n",
        " false                      \n",
        " false                      \n",
        "  true                      \n",
        "      Bool[true,false,false]"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [
      "{is_zero(1), is_zero(0), is_zero([-1,0,1])}"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 4,
       "text": [
        "3-element Array{Any,1}:\n",
        " false                      \n",
        "  true                      \n",
        "      Bool[false,true,false]"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [
      "{is_one(1), is_one(0), is_one([-1,0,1])}"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 5,
       "text": [
        "3-element Array{Any,1}:\n",
        "  true                      \n",
        " false                      \n",
        "      Bool[false,false,true]"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "iterate"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "take(5, iterate(inc, 0)) |> collect"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 6,
       "text": [
        "5-element Array{Any,1}:\n",
        " 0\n",
        " 1\n",
        " 2\n",
        " 3\n",
        " 4"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "mapcat"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "mapcat(inc, {[1,2,3], [4,5,6]})"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 7,
       "text": [
        "6-element Array{Int64,1}:\n",
        " 2\n",
        " 3\n",
        " 4\n",
        " 5\n",
        " 6\n",
        " 7"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "filter"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "it = lfilter(isodd, 1:10)\n",
      "collect(it)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 8,
       "text": [
        "5-element Array{Any,1}:\n",
        " 1\n",
        " 3\n",
        " 5\n",
        " 7\n",
        " 9"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "it = lfilter(iseven, 1:10)\n",
      "collect(it)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 9,
       "text": [
        "5-element Array{Any,1}:\n",
        "  2\n",
        "  4\n",
        "  6\n",
        "  8\n",
        " 10"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "partition"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "partition(3, 1:10)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 10,
       "text": [
        "3-element Array{UnitRange{Int64},1}:\n",
        " 1:3\n",
        " 4:6\n",
        " 7:9"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "it = partition(3, take(10, 1:10))\n",
      "collect(it)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 11,
       "text": [
        "3-element Array{Any,1}:\n",
        " {1,2,3}\n",
        " {4,5,6}\n",
        " {7,8,9}"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "partition_all"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "partition_all(3, [1:10])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 12,
       "text": [
        "4-element Array{Array{Int64,1},1}:\n",
        " [1,2,3]\n",
        " [4,5,6]\n",
        " [7,8,9]\n",
        " [10]   "
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "it = partition_all(3, take(10, 1:10))\n",
      "collect(it)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 13,
       "text": [
        "4-element Array{Any,1}:\n",
        " {1,2,3}\n",
        " {4,5,6}\n",
        " {7,8,9}\n",
        " {10}   "
       ]
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "take"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "it = take(0, [1,2,3])\n",
      "collect(it)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 14,
       "text": [
        "0-element Array{Any,1}"
       ]
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "drop"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "it = drop(0, [1,2,3])\n",
      "collect(it)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 15,
       "text": [
        "3-element Array{Any,1}:\n",
        " 1\n",
        " 2\n",
        " 3"
       ]
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "lconcat"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "it = lconcat([1,2,3], [4,5,6], [7,8,9])\n",
      "collect(it)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 16,
       "text": [
        "9-element Array{Any,1}:\n",
        " 1\n",
        " 2\n",
        " 3\n",
        " 4\n",
        " 5\n",
        " 6\n",
        " 7\n",
        " 8\n",
        " 9"
       ]
      }
     ],
     "prompt_number": 16
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "interleave"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "interleave([1,2,3])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 17,
       "text": [
        "3-element Array{Int64,1}:\n",
        " 1\n",
        " 2\n",
        " 3"
       ]
      }
     ],
     "prompt_number": 17
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "interleave([1.,2,3], [4.,5,6])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 18,
       "text": [
        "6-element Array{Float64,1}:\n",
        " 1.0\n",
        " 4.0\n",
        " 2.0\n",
        " 5.0\n",
        " 3.0\n",
        " 6.0"
       ]
      }
     ],
     "prompt_number": 18
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "interleave([1.,2,3], [4.,5,6], [7.,8,9])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 19,
       "text": [
        "9-element Array{Float64,1}:\n",
        " 1.0\n",
        " 4.0\n",
        " 7.0\n",
        " 2.0\n",
        " 5.0\n",
        " 8.0\n",
        " 3.0\n",
        " 6.0\n",
        " 9.0"
       ]
      }
     ],
     "prompt_number": 19
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "interleave([1, 2, 3], [4, 5])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 20,
       "text": [
        "4-element Array{Int64,1}:\n",
        " 1\n",
        " 4\n",
        " 2\n",
        " 5"
       ]
      }
     ],
     "prompt_number": 20
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "interleave([1, 2], [3, 4, 5])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 21,
       "text": [
        "4-element Array{Int64,1}:\n",
        " 1\n",
        " 3\n",
        " 2\n",
        " 4"
       ]
      }
     ],
     "prompt_number": 21
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "it = iterate(inc, 1)\n",
      "interleave(it)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 22,
       "text": [
        "IterateIterator{Int64}(inc,1)"
       ]
      }
     ],
     "prompt_number": 22
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "it1 = take(3, iterate(inc, 1))\n",
      "it2 = take(3, iterate(dec, -1))\n",
      "collect(Int64, interleave(it1, it2))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 23,
       "text": [
        "6-element Array{Int64,1}:\n",
        "  1\n",
        " -1\n",
        "  2\n",
        " -2\n",
        "  3\n",
        " -3"
       ]
      }
     ],
     "prompt_number": 23
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "it1 = take(3, iterate(inc, 1))\n",
      "it2 = take(2, iterate(dec, -1))\n",
      "collect(Int64, interleave(it1, it2))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 24,
       "text": [
        "4-element Array{Int64,1}:\n",
        "  1\n",
        " -1\n",
        "  2\n",
        " -2"
       ]
      }
     ],
     "prompt_number": 24
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "it1 = take(2, iterate(inc, 1))\n",
      "it2 = take(3, iterate(dec, -1))\n",
      "collect(Int64, interleave(it1, it2))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 25,
       "text": [
        "4-element Array{Int64,1}:\n",
        "  1\n",
        " -1\n",
        "  2\n",
        " -2"
       ]
      }
     ],
     "prompt_number": 25
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "it1 = iterate(inc, 1)\n",
      "it2 = iterate(dec, -1)\n",
      "interleaved = interleave(it1, it2)\n",
      "collect(Int64, take(6, interleaved))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 26,
       "text": [
        "6-element Array{Int64,1}:\n",
        "  1\n",
        " -1\n",
        "  2\n",
        " -2\n",
        "  3\n",
        " -3"
       ]
      }
     ],
     "prompt_number": 26
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "it = enumerate(take(5, lmap(x->2x, iterate(inc, 1))))\n",
      "collect(it)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 27,
       "text": [
        "5-element Array{(Int64,Any),1}:\n",
        " (1,2) \n",
        " (2,4) \n",
        " (3,6) \n",
        " (4,8) \n",
        " (5,10)"
       ]
      }
     ],
     "prompt_number": 27
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "separate"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "it = interleave([1,2,3], [4,5,6])\n",
      "separate(2, it)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 28,
       "text": [
        "2-element Array{Array{Int64,1},1}:\n",
        " [1,2,3]\n",
        " [4,5,6]"
       ]
      }
     ],
     "prompt_number": 28
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "it = interleave(take(3, [1,2,3]), take(3, [4,5,6]))\n",
      "separate(2, it) |> (c -> map(x -> collect(Int64, x), c))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 29,
       "text": [
        "2-element Array{Array{Int64,1},1}:\n",
        " [1,2,3]\n",
        " [4,5,6]"
       ]
      }
     ],
     "prompt_number": 29
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "it = interleave(take(3, [1,2,3]), take(3, [4,5,6]))\n",
      "separate(1, it) |> (c -> map(x -> collect(Int64, x), c))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 30,
       "text": [
        "1-element Array{Array{Int64,1},1}:\n",
        " [1,4,2,5,3,6]"
       ]
      }
     ],
     "prompt_number": 30
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "split_at"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "split_at(0, [1,2,3,4])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 31,
       "text": [
        "2-element Array{Array{Int64,1},1}:\n",
        " []       \n",
        " [1,2,3,4]"
       ]
      }
     ],
     "prompt_number": 31
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "split_at(1, [1,2,3,4])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 32,
       "text": [
        "2-element Array{Array{Int64,1},1}:\n",
        " [1]    \n",
        " [2,3,4]"
       ]
      }
     ],
     "prompt_number": 32
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "split_at(2, [1,2,3,4])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 33,
       "text": [
        "2-element Array{Array{Int64,1},1}:\n",
        " [1,2]\n",
        " [3,4]"
       ]
      }
     ],
     "prompt_number": 33
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "l, r = split_at(0, take(4, [1,2,3,4]))\n",
      "(collect(l), collect(r))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 34,
       "text": [
        "({},{1,2,3,4})"
       ]
      }
     ],
     "prompt_number": 34
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "l, r = split_at(1, take(4, [1,2,3,4]))\n",
      "(collect(l), collect(r))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 35,
       "text": [
        "({1},{2,3,4})"
       ]
      }
     ],
     "prompt_number": 35
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "l, r = split_at(2, take(4, [1,2,3,4]))\n",
      "(collect(l), collect(r))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 36,
       "text": [
        "({1,2},{3,4})"
       ]
      }
     ],
     "prompt_number": 36
    }
   ],
   "metadata": {}
  }
 ]
}